﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyPublisher.Windows.Controls
{
    [
        TemplatePart(Name = UpDownButton.ElementGrid, Type = typeof(Grid)),
        TemplatePart(Name = UpDownButton.ElementUpButton, Type = typeof(Button)),
        TemplatePart(Name = UpDownButton.ElementDownButton, Type = typeof(Button))
    ]
    public class UpDownButton : Control
    {
        public const string ElementGrid = "PART_Grid";
        public const string ElementUpButton = "PART_UpButton";
        public const string ElementDownButton = "PART_DownButton";

        private Button _upButton;
        private Button _downButton;

        public static readonly RoutedEvent IncreaseEvent = EventManager.RegisterRoutedEvent(
            "Increase", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UpDownButton));
        public event RoutedEventHandler Increase
        {
            add { AddHandler(IncreaseEvent, value); }
            remove { RemoveHandler(IncreaseEvent, value); }
        }

        public static readonly RoutedEvent DecreaseEvent = EventManager.RegisterRoutedEvent(
            "Decrease", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UpDownButton));
        public event RoutedEventHandler Decrease
        {
            add { AddHandler(DecreaseEvent, value); }
            remove { RemoveHandler(DecreaseEvent, value); }
        }

        static UpDownButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UpDownButton), new FrameworkPropertyMetadata(typeof(UpDownButton)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _upButton = GetTemplateChild(ElementUpButton) as Button;
            _downButton = GetTemplateChild(ElementDownButton) as Button;

            _upButton.Click += new RoutedEventHandler(_upButton_Click);
            _downButton.Click += new RoutedEventHandler(_downButton_Click);
        }

        void _downButton_Click(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(DecreaseEvent, this));
        }

        void _upButton_Click(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(IncreaseEvent, this));
        }

        
    }
}
